package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.StickyEventBan;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.CarAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.DayAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyAvoidExplodeAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyCheck1ListAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyFestivalDuty2ListAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.NightAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.FestivalDuty2Bean;
import patrol.mobile.com.mobilepatrol.bean.FestivalDutyBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by K on 2018/9/14.
 */
public class EmergencyFestivalDuty2Activity extends BaseActivity {

    private LinearLayout llBaiTian;
    private LinearLayout llYeBan;
    private LinearLayout llCheLiang;

    private RecyclerView recyclerView;

    private DayAdapter dayAdapter;
    private NightAdapter nightAdapter;
    private CarAdapter carAdapter;
    private String id = "";
    @BindView(R.id.tab_layout_other_business)
    TabLayout tabLayoutOtherBusiness;
    @BindView(R.id.vp_other_business)
    ViewPager vpOtherBusiness;

    @Override
    public void initView() {
        super.initView();
        llBaiTian = findViewById(R.id.ll_baiTian);
        llYeBan = findViewById(R.id.ll_yeBan);
        llCheLiang = findViewById(R.id.ll_cheLiang);
        recyclerView = findViewById(R.id.recyclerView);

    }

    @Override
    public void initData() {
        super.initData();

       TabFragmentAdapter adapter = new TabFragmentAdapter(getSupportFragmentManager(), this);
        vpOtherBusiness.setAdapter(adapter);
        tabLayoutOtherBusiness.setupWithViewPager(vpOtherBusiness);
        vpOtherBusiness.setOffscreenPageLimit(3);
        switchTab(0);
    }

    private void switchTab(int index) {
        if (vpOtherBusiness != null) {
            vpOtherBusiness.setCurrentItem(index, false);
        }
    }
    private Fragment[] tabs;
    /*
     * 此结构为activity+fragment+fragment
     */
    class TabFragmentAdapter extends FragmentStatePagerAdapter {
        private String[] TITLES;



        private TabFragmentAdapter(FragmentManager fm, Activity context) {
            super(fm);
            TITLES = new String[]{"白班","夜班","车辆"};
            tabs = new Fragment[TITLES.length];
            tabs[0] = new BaiBanFragment();
            tabs[1] = new YeBanBanFragment();
            tabs[2] = new CheLiangFragment();
        }

        @Override
        public Fragment getItem(int position) {
            return tabs[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }
    }


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_emergency_festival_duty2_top;
    }

    @Override
    public void setListener() {
        llBaiTian.setBackgroundColor(getResources().getColor(R.color.gray));
        llYeBan.setBackgroundColor(getResources().getColor(R.color.transparent));
        llCheLiang.setBackgroundColor(getResources().getColor(R.color.transparent));
        id = getIntent().getStringExtra("id");
        requestData(0, id);

        vpOtherBusiness.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(EmergencyFestivalDuty2Activity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(ConsTitles.EMERGENCY_FESTIVAL_DUTY_2);
    }


    @OnClick({R.id.ll_baiTian, R.id.ll_yeBan, R.id.ll_cheLiang})
    public void onck(View v) {

        switch (v.getId()) {

            case R.id.ll_baiTian:
                llBaiTian.setBackgroundColor(getResources().getColor(R.color.gray));
                llYeBan.setBackgroundColor(getResources().getColor(R.color.transparent));
                llCheLiang.setBackgroundColor(getResources().getColor(R.color.transparent));

                requestData(0, id);
                break;
            case R.id.ll_yeBan:
                llBaiTian.setBackgroundColor(getResources().getColor(R.color.transparent));
                llYeBan.setBackgroundColor(getResources().getColor(R.color.gray));
                llCheLiang.setBackgroundColor(getResources().getColor(R.color.transparent));

                requestData(1, id);
                break;
            case R.id.ll_cheLiang:
                llBaiTian.setBackgroundColor(getResources().getColor(R.color.transparent));
                llYeBan.setBackgroundColor(getResources().getColor(R.color.transparent));
                llCheLiang.setBackgroundColor(getResources().getColor(R.color.gray));

                requestData(2, id);
                break;
        }

    }


    private void requestData(final int index, String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.EMERGENCY_FESTIVAL_DUTY_2);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                FestivalDuty2Bean festivalDuty2Bean = GsonUtil.GsonToBean(response, FestivalDuty2Bean.class);

                EventBus.getDefault().postSticky(new StickyEventBan(festivalDuty2Bean));

//                List<FestivalDuty2Bean.AbkHolidayRotaDayListBean> abkHolidayRotaDayListBeans = festivalDuty2Bean.getAbkHolidayRotaDayList();
//                List<FestivalDuty2Bean.AbkHolidayRotaNightListBean> abkHolidayRotaNightListBeans = festivalDuty2Bean.getAbkHolidayRotaNightList();
//                List<FestivalDuty2Bean.AbkHolidayRotaCarListBean> abkHolidayRotaCarListBeans = festivalDuty2Bean.getAbkHolidayRotaCarList();
//
//                if (index == 0) {
//                    dayAdapter = new DayAdapter(EmergencyFestivalDuty2Activity.this);
//                    RecyclerViewHelper.initRecyclerViewV(EmergencyFestivalDuty2Activity.this, recyclerView,
//                            false, dayAdapter);
//                    recyclerView.setAdapter(dayAdapter);
//                    dayAdapter.updateItems(abkHolidayRotaDayListBeans);
//
//                } else if (index == 1) {
//                    nightAdapter = new NightAdapter(EmergencyFestivalDuty2Activity.this);
//                    RecyclerViewHelper.initRecyclerViewV(EmergencyFestivalDuty2Activity.this, recyclerView,
//                            false, nightAdapter);
//                    recyclerView.setAdapter(nightAdapter);
//                    nightAdapter.updateItems(abkHolidayRotaNightListBeans);
//                } else if (index == 2) {
//                    carAdapter = new CarAdapter(EmergencyFestivalDuty2Activity.this);
//                    RecyclerViewHelper.initRecyclerViewV(EmergencyFestivalDuty2Activity.this, recyclerView,
//                            false, carAdapter);
//                    recyclerView.setAdapter(carAdapter);
//                    carAdapter.updateItems(abkHolidayRotaCarListBeans);
//                }
            }
        });
    }

}
